package business {
	import mx.collections.ArrayList;

	public class CurrencyContainer {
		private static var _instance:CurrencyContainer;
		private var _currencies:ArrayList;
		private var _dateOfRates:String;

		[Bindable]
		public var fromCurrency:Currency;
		[Bindable]
		public var toCurrency:Currency;
		[Bindable]
		public var fromAmount:String = "1";
		[Bindable]
		public var resultAmount:String;

		public function CurrencyContainer(access:Private) {
			if (access == null) {
				throw new Error("This is a singleton.. Use the getInstance method");
			}
			_currencies = new ArrayList();
			addCurrency(new Currency("EUR", "EUR", 1));
		}

		[Bindable]
		public function set dateOfRates(value:String):void {
			_dateOfRates = value;
		}

		public function get dateOfRates():String {
			return _dateOfRates;
		}

		public function get currencies():ArrayList {
			return _currencies;
		}

		public function addCurrency(currency:Currency):void {
			if (currency != null) {
				currencies.addItem(currency);
				if (fromCurrency == null)
					fromCurrency = currency;
				else if (toCurrency == null)
					toCurrency = currency;
			}
		}

		public function getCurrency(currencyCode:String):Currency {
			var currency:Currency = null;
			var found:Boolean = false;

			for (var i:int = 0; !found && i < currencies.length; i++) {
				var cur:Currency = Currency(currencies.getItemAt(i));
				if (cur.code == currencyCode) {
					currency = cur;
					found = true;
				}
			}
			return currency;
		}

		public static function getInstance():CurrencyContainer {
			if (_instance == null) {
				_instance = new CurrencyContainer(new Private);
			}
			return _instance;
		}
	}
}

class Private {
}
